<template>
  <div class="bg">
    <div class="login">
      <h3 class="h3">叩丁狼后台管理系统</h3>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" type="input" autocomplete="off">
            <template #prefix>
              <el-icon>
                <Avatar />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
          >
            <template #prefix>
              <el-icon>
                <Unlock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import { Avatar, Unlock } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { loginApi } from "../login/api";
import { useRouter } from "vue-router";
const router = useRouter();

const ruleFormRef = ref<FormInstance>();

const ruleForm = reactive({
  username: "",
  password: "",
});

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else if (value !== ruleForm.password) {
    callback(new Error("Two inputs don't match!"));
  } else {
    callback();
  }
};

const rules = reactive({
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ validator: validatePass, trigger: "blur" }],
});

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      loginApi(ruleForm).then((res) => {
        if (res.code === 200) {
          // console.log("登录成功");

          localStorage.setItem("token", res.data.tokenHead + res.data.token);
          ElMessage.success("登录成功");
          router.push("/index");
          // 存token
        }
      });
    } else {
      ElMessage.error("请填写正确的账号和密码");
      return false;
    }
  });
};
console.log(router.getRoutes());

</script>

<style scoped lang="less">
.bg {
  position: absolute;
  height: 100%;
  width: 100%;
  background-color: #2d3a4b;

  .login {
    width: 400px;
    margin: 250px auto 0;

    .h3 {
      width: 100%;
      margin-bottom: 20px;
      text-align: center;
      color: rgb(255, 255, 255);
    }

    :deep(.el-button--primary) {
      width: 100%;
      height: 40px;
    }

    :deep(.el-input__wrapper) {
      border: 1px solid hsla(0, 0%, 100%, 0.1);
      background-color: rgba(0, 0, 0, 0.1);
    }
  }
}
</style>
